<!DOCTYPE html>
<html class="L_UI">
<head>
    <title></title>
    <link href="/CSS/Main.css" rel="stylesheet" type="text/css" />
    <link href="/App_Themes/MSN_Blue/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="Window">
        <div class="Content" style="top: 0px;">
            <table id="table1" class="cont_list" style="width:100%;border-collapse:collapse;" cellspacing="0" border="1">
                <thead>
                    <tr>
                        <th id="title" colspan="11" style="font-size:16px;"></th>
                    </tr>
                    <tr>
                        <th style="width: 50px;">
                            周
                        </th>
                        <th>
                            计划流入合计
                        </th>
                        <th>
                            实际流入合计
                        </th>
                        <th>
                            计划流入次数统计
                        </th>
                        <th>
                            实际流入次数统计
                        </th>
                        <th>
                            流入率
                        </th>
                        <th>
                            计划流出合计
                        </th>
                        <th>
                            实际流出合计
                        </th>
                        <th>
                            计划流出次数统计
                        </th>
                        <th>
                            实际流出次数统计
                        </th>
                        <th>
                            流出率
                        </th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
                <tfoot>
                    <tr>
                        <th>
                            合计：
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th>
                            -
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th style="text-align:right;padding-right:10px;">
                        </th>
                        <th>
                            -
                        </th>
                    </tr>
                    <tr>
                        <th colspan="11" style="text-align:right;padding-right:10px;color:Red;font-size:12px;">
                            <a id="Print_Btn" style="color:Blue;font-size:5px;padding-right:5px;">打印此报表、</a>金额单位(万元)保留4位有效小数
                        </th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <script src="/Scripts/jquery.1.11.1.js" type="text/javascript"></script>
    <script>
        var app1 = {};
        app1.Ajax = $.post;
        app1.SqlHelper = {
            Query: function (sql, callback) {
                $.post("/Funds/PlanWeekFunds1.aspx?Ajax=1", JSON.stringify({
                    Code: "Query",
                    Data: {
                        Sql: sql
                    }
                }), function (data) {
                    if (data.Type == 1) {
                        callback(data.Data);
                    }
                })
            },
            NoQuery: function (sql, callback) {
                $.post("/Funds/PlanWeekFunds1.aspx?Ajax=1", JSON.stringify({
                    Code: "NoQuery",
                    Data: {
                        Sql: sql
                    }
                }), function (data) {
                    if (data.Type == 1) {
                        callback(data.Data);
                    }
                })
            }
        };
        app1.GetRequest = function (ifObj) {
            if (typeof ifObj == 'boolean') {
                if (ifObj) {
                    var obj = {};
                    obj.Data = this.GetRequest();
                    obj.QueryString = function (key) {
                        for (var item in obj.Data) {
                            if (item.toLocaleLowerCase() == key.toLocaleLowerCase()) {
                                return obj.Data[item];
                            }
                        }
                        return '';
                    }
                    return obj;
                }
            }
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        app1.QueryString = app1.GetRequest(true).QueryString;
        app1.Data = {};
        app1.Init = function () {
            //装载数据
            var entity = this.Data.Data.Entity;
            var company = entity.Company;
            //设置标题
            //console.log(entity);
            title = entity.Year + "年" + entity.Month + "月(" + company.Name + ")资金明细报表";
            $("#title").html(title);
            var data = this.Data.Data.Data;
            var footerEntity = {};
            footerEntity.USER_AMOUNT_INTO = 0;
            footerEntity.USER_AMOUNT_INTO_ACTUAL = 0;
            footerEntity.USER_AMOUNT_INTO_NUMBER = 0;
            footerEntity.USER_AMOUNT_INTO_NUMBER_ACTUAL = 0;
            footerEntity.USER_AMOUNT_OUT = 0;
            footerEntity.USER_AMOUNT_OUT_ACTUAL = 0;
            footerEntity.USER_AMOUNT_OUT_NUMBER = 0;
            footerEntity.USER_AMOUNT_OUT_NUMBER_ACTUAL = 0;
            var dom = $("#table1 tbody");
            $.each(data, function (index, item) {
                var html = "<tr>";

                if (item.USER_AMOUNT_INTO == null) {
                    item.USER_AMOUNT_INTO = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_INTO = parseFloat(item.USER_AMOUNT_INTO);
                }
                if (item.USER_AMOUNT_INTO_ACTUAL == null) {
                    item.USER_AMOUNT_INTO_ACTUAL = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_INTO_ACTUAL = parseFloat(item.USER_AMOUNT_INTO_ACTUAL);
                }
                if (item.USER_AMOUNT_INTO_NUMBER == null) {
                    item.USER_AMOUNT_INTO_NUMBER = 0;
                }
                else {
                    item.USER_AMOUNT_INTO_NUMBER = parseInt(item.USER_AMOUNT_INTO_NUMBER);
                }
                if (item.USER_AMOUNT_INTO_NUMBER_ACTUAL == null) {
                    item.USER_AMOUNT_INTO_NUMBER_ACTUAL = 0;
                }
                else {
                    item.USER_AMOUNT_INTO_NUMBER_ACTUAL = parseInt(item.USER_AMOUNT_INTO_NUMBER_ACTUAL);
                }
                if (item.USER_AMOUNT_INTO == null) {
                    item.USER_AMOUNT_INTO = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_INTO = parseFloat(item.USER_AMOUNT_INTO);
                }
                if (item.USER_AMOUNT_OUT == null) {
                    item.USER_AMOUNT_OUT = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_OUT = parseFloat(item.USER_AMOUNT_OUT);
                }
                if (item.USER_AMOUNT_OUT_ACTUAL == null) {
                    item.USER_AMOUNT_OUT_ACTUAL = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_OUT_ACTUAL = parseFloat(item.USER_AMOUNT_OUT_ACTUAL);
                }
                if (item.USER_AMOUNT_OUT_NUMBER == null) {
                    item.USER_AMOUNT_OUT_NUMBER = 0;
                }
                else {
                    item.USER_AMOUNT_OUT_NUMBER = parseInt(item.USER_AMOUNT_OUT_NUMBER);
                }
                if (item.USER_AMOUNT_OUT_NUMBER_ACTUAL == null) {
                    item.USER_AMOUNT_OUT_NUMBER_ACTUAL = 0;
                }
                else {
                    item.USER_AMOUNT_OUT_NUMBER_ACTUAL = parseInt(item.USER_AMOUNT_OUT_NUMBER_ACTUAL);
                }
                if (item.USER_AMOUNT_OUT == null) {
                    item.USER_AMOUNT_OUT = parseFloat(0);
                }
                else {
                    item.USER_AMOUNT_OUT = parseFloat(item.USER_AMOUNT_OUT);
                }

                html += "<td>" + item.WEEK.toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_INTO.toFixed(4).toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_INTO_ACTUAL.toFixed(4).toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_INTO_NUMBER.toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_INTO_NUMBER_ACTUAL.toString() + "</td>";
                var kk = "0.00";
                if (item.USER_AMOUNT_INTO != 0) {
                    kk = (item.USER_AMOUNT_INTO_ACTUAL / item.USER_AMOUNT_INTO * 100).toFixed(2).toString();
                }
                html += "<td style=\"text-align:right;padding-right:10px;\">" + kk + "%</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_OUT.toFixed(4).toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_OUT_ACTUAL.toFixed(4).toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_OUT_NUMBER.toString() + "</td>";
                html += "<td style=\"text-align:right;padding-right:10px;\">" + item.USER_AMOUNT_OUT_NUMBER_ACTUAL.toString() + "</td>";
                kk = "0.00";
                if (item.USER_AMOUNT_OUT != 0) {
                    kk = (item.USER_AMOUNT_OUT_ACTUAL / item.USER_AMOUNT_OUT * 100).toFixed(2).toString();
                }
                html += "<td>" + kk + "%</td>";
                html += "</tr>";
                footerEntity.USER_AMOUNT_INTO += item.USER_AMOUNT_INTO;
                footerEntity.USER_AMOUNT_INTO_ACTUAL += item.USER_AMOUNT_INTO_ACTUAL;
                footerEntity.USER_AMOUNT_INTO_NUMBER += item.USER_AMOUNT_INTO_NUMBER;
                footerEntity.USER_AMOUNT_INTO_NUMBER_ACTUAL += item.USER_AMOUNT_INTO_NUMBER_ACTUAL;
                footerEntity.USER_AMOUNT_OUT += item.USER_AMOUNT_OUT;
                footerEntity.USER_AMOUNT_OUT_ACTUAL += item.USER_AMOUNT_OUT_ACTUAL;
                footerEntity.USER_AMOUNT_OUT_ACTUAL += item.USER_AMOUNT_OUT_ACTUAL;
                footerEntity.USER_AMOUNT_OUT_NUMBER += item.USER_AMOUNT_OUT_NUMBER;
                footerEntity.USER_AMOUNT_OUT_NUMBER_ACTUAL += item.USER_AMOUNT_OUT_NUMBER_ACTUAL;

                dom.append(html);
            });
            var footerDom = $("#table1 tfoot tr th");
            $(footerDom[1]).html(footerEntity.USER_AMOUNT_INTO.toFixed(4).toString());
            $(footerDom[2]).html(footerEntity.USER_AMOUNT_INTO_ACTUAL.toFixed(4).toString());
            $(footerDom[3]).html(footerEntity.USER_AMOUNT_INTO_NUMBER.toString());
            $(footerDom[4]).html(footerEntity.USER_AMOUNT_INTO_NUMBER_ACTUAL.toString());
            $(footerDom[6]).html(footerEntity.USER_AMOUNT_OUT.toFixed(4).toString());
            $(footerDom[7]).html(footerEntity.USER_AMOUNT_OUT_ACTUAL.toFixed(4).toString());
            $(footerDom[8]).html(footerEntity.USER_AMOUNT_OUT_NUMBER.toString());
            $(footerDom[9]).html(footerEntity.USER_AMOUNT_OUT_NUMBER_ACTUAL.toString());
        };
        app1.Service = {
            Get: function (applyId, callback) {
                app1.SqlHelper.Query("SELECT * FROM QS_PLANMONTHAPPLY T1 WHERE T1.N_MONTHAPP_ID=" + applyId.toString(), function (result1) {
                    console.log(result1);
                    if (result1.length == 1) {
                        var current = result1[0];
                        var entity = {};
                        //app1.Data.Data.Entity = entity;
                        entity.entity = current;
                        entity.ApplyId = parseInt(current.N_MONTHAPP_ID);
                        entity.Year = parseInt(current.N_YEAR);
                        entity.Month = parseInt(current.N_MONTH);
                        entity.CompanyId = parseInt(current.N_COMPANY_ID);

                        app1.SqlHelper.Query("SELECT * FROM SYS_COMPANY T1 WHERE T1.CMY_ID=" + entity.CompanyId, function (result2) {
                            if (result2.length == 1) {
                                var current2 = result2[0];
                                var entity2 = {};
                                entity2.Name = current2.CMY_NAME;
                                entity2.Id = parseInt(current2.CMY_ID);
                                entity.Company = entity2;
                                var sql3 = "SELECT";
                                sql3 += " T1.N_WEEK AS WEEK,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,1,T2.N_AMOUNT,0)) AS USER_AMOUNT_INTO,";
                                sql3 += " (SUM(DECODE(T3.N_TYPE,1,DECODE(T4.N_AMT,NULL,0,T4.N_AMT),0))/10000) AS USER_AMOUNT_INTO_ACTUAL,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,1,1,0)) AS USER_AMOUNT_INTO_NUMBER,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,1,DECODE(T4.N_AMT,NULL,0,1),0)) AS USER_AMOUNT_INTO_NUMBER_ACTUAL,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,2,T2.N_AMOUNT,0)) AS USER_AMOUNT_OUT,";
                                sql3 += " (SUM(DECODE(T3.N_TYPE,2,DECODE(T4.N_AMT,NULL,0,T4.N_AMT),0))/10000) AS USER_AMOUNT_OUT_ACTUAL,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,2,1,0)) AS USER_AMOUNT_OUT_NUMBER,";
                                sql3 += " SUM(DECODE(T3.N_TYPE,2,DECODE(T4.N_AMT,NULL,0,1),0)) AS USER_AMOUNT_OUT_NUMBER_ACTUAL";
                                sql3 += " FROM";
                                sql3 += " QS_PLANWEEKAPPLY T1";
                                sql3 += " INNER JOIN QS_PLANWEEK_DETAIL T2 ON T1.N_WEEKAPP_ID = T2.N_WEEKAPP_ID";
                                sql3 += " INNER JOIN QS_PROJECT T3 ON T2.N_PROID = T3.N_PROID";
                                sql3 += " LEFT JOIN YX_BANK_DETAILLIST T4";
                                sql3 += " ON T2.C_PROWEEKNUM = T4.C_PROWEEKNUM";
                                sql3 += " WHERE T1.N_MONTHAPP_ID = " + entity.ApplyId.toString() + " AND T2.N_TYPE =2";
                                sql3 += " GROUP BY ";
                                sql3 += " T1.N_WEEK";
                                sql3 += " ORDER BY T1.N_WEEK";
                                app1.SqlHelper.Query(sql3, function (result3) {
                                    //app1.Data.Data.Data = result3.Data;
                                    //console.log(sql3);
                                    //console.log(result3);
                                    callback(entity, result3);
                                })
                            }
                        })

                    }
                });
            }
        };
        app1.PrintClick = function () {
            $("#table").hide();
        }
        app1.Load = function () {
            this.Data.Id = this.QueryString("ID");
            if (this.Data.Id == undefined || this.Data.Id == "" || this.Data.Id == null) {
                alert("参数传递错误");
                setTimeout(function () {
                    location.href = "/Content/Erro.html";
                }, 5000);
                return;
            }
            var that = this;
            this.Service.Get(this.Data.Id, function (entity, data) {
                that.Data.Data = {};
                that.Data.Data.Entity = entity;
                that.Data.Data.Data = data;
                that.Init();
                $("#Print_Btn").click = that.PrintClick;
            })
        }
        $(function () {
            app1.Load();
        })
    </script>
</body>
</html>